var Bill = (function () { 
	var me = this;
	me.items  	= [] ;
	me.t_items 		= 0;
	me.t_price		= 0.00;
	me.t_scharge 	= 0.00;
	me.t_tax		= 0.00;
	me.t_discount	= 0.00;
	me.t_grand		= 0.00;
	
	me.format_c		= { symbol: "IDR",  format: "%v %s" };
	me.format_d		= { format: "%v" };
	
	me._target_t_items 		= '#t_items';
	me._target_t_price 		= '#t_price';
	me._target_t_scharge 	= '#t_scharge';
	me._target_t_discount 	= '#t_discount';
	me._target_t_tax 		= '#t_tax';
	me._target_t_grand 		= '#t_grand';
	
	me._target_table		= '#tbl_sales';
	me._target_tbody_sales	= "#tbody_sales";
	
	
	
	me.init = function(){
		me.reset();
	}
	
	
	/*------------EMPTY bill---------------------------*/
	me.reset = function () {
		me.items = [];
		me.refresh_table();
		me.reset_summary();
		me.refresh_summary();
	};
	
	me.reset_summary = function(){
		me.t_items 		= 0;
		me.t_price		= accounting.formatMoney(0.00,me.format_c);
		me.t_scharge 	= accounting.formatMoney(0.00,me.format_c);
		me.t_tax		= accounting.formatMoney(0.00,me.format_c);
		me.t_discount	= accounting.formatMoney(0.00,me.format_c);
		me.t_grand		= accounting.formatMoney(0.00,me.format_c);
	}
	me.refresh_summary = function(){
		$(me._target_t_items).text('0'); 
		$(me._target_t_price).text(accounting.formatMoney(0.00,me.format_c));
		$(me._target_t_scharge).text(accounting.formatMoney(0.00,me.format_c));
		$(me._target_t_tax).text(accounting.formatMoney(0.00,me.format_c));
		$(me._target_t_discount).text(accounting.formatMoney(0.00,me.format_c));
		$(me._target_t_grand).text(accounting.formatMoney(0.00,me.format_c));
	}
	
	me.truncate_str = function(s){
	var tmp = '';
	if (s.length>24){
		tmp = s.substr(0,20)+'...';
		return tmp
	} else {tmp = s; return tmp}
	}
	
	/*------------Display items on table----------------*/
 	me.refresh_table = function(){
		$(me._target_tbody_sales).empty();		
		$.each(me.items,function(i){ 
			var newTr=$('<tr id="row_'+me.items[i].id+'"></tr>');
				row 	= '<td  style="width: 10%;">';
				row		+= '<button id="del-'+me.items[i].id+'" value="'+me.items[i].id+'" class="warning icon-cancel"></button>';
				row		+= '</td>';
				row		+= '<td style="width: 30%;">'+me.truncate_str(me.items[i].name)+'</td>';
				row		+= '<td style="width: 15%;text-align:right"><span id="qty">'+me.items[i].qty+'</span></td>';
				row		+= '<td style="width: 20%;text-align:right " >'+ accounting.formatMoney(me.items[i].price * me.items[i].qty,me.format_c) +'</td>';
			
				newTr.html(row);
				newTr.appendTo(me._target_table); 
		});
	}
	
	
	/*----display summary-----*/
	me.display_summary = function(){
	
		$(me._target_t_items).text(me.t_items); 
		$(me._target_t_price).text(accounting.formatMoney(me.t_price.toFixed(2),me.format_c));
		$(me._target_t_scharge).text(accounting.formatMoney(me.t_scharge.toFixed(2),me.format_c));
		$(me._target_t_tax).text(accounting.formatMoney(me.t_tax.toFixed(2),me.format_c));
		$(me._target_t_discount).text(accounting.formatMoney(me.t_discount.toFixed(2),me.format_c));
		$(me._target_t_grand).text(accounting.formatMoney(me.t_grand.toFixed(2),me.format_c));
		
	}
	
	me.add_item = function (newItem){
		//alert('newItem.qty : '+newItem.qty);
		//console.log(me.items);
		//alert(me.search_indexbyid(newItem.id));
		if (!(me.search_indexbyid(newItem.id)==-1)){
			var i = me.search_indexbyid(newItem.id); 
			//me.items[i].qty += 1; 
			me.items[i].qty +=  newItem.qty;
			
		} else {
			me.items.push(newItem);
		}	
				
		/*calculate summary*/
		me.recalc_total();
		
		/*refresh table*/
		me.refresh_table()
		
		/*refresh summary*/
		me.display_summary();
		//console.log(JSON.parse(JSON.stringify(me.items)));
		return null;
	}
	
	me.remove_item = function (id){
		var idx = me.objectIdx_FindByKey(me.items,'id',id);
		//alert('idx : '+idx);
		me.items.splice(idx,1);
				
		/*calculate summary*/
		me.recalc_total();
		
		/*refresh table*/
		me.refresh_table()
		
		/*refresh summary*/
		me.display_summary();
		
		return null;
	}
	
	me.recalc_total = function (){
		//reseting total first 
		me.t_items 		= 0;
		me.t_price		= 0.00;
		me.t_scharge 	= 0.00;
		me.t_tax		= 0.00;
		me.t_discount	= 0.00;
		me.t_grand		= 0.00;
		
		$.each(me.items,function(i){
		//alert(me.items[i]['qty']);
			me.t_items		= me.t_items + me.items[i]['qty'];
			me.t_price		= me.t_price + (me.items[i]['qty']*me.items[i]['price']);
			me.t_scharge	= me.t_scharge + (me.items[i]['scharge']*(me.items[i]['qty']*me.items[i]['price'])/100);
			me.t_tax		= me.t_tax + ( me.items[i]['tax']*(    (me.items[i]['scharge']*(me.items[i]['qty']*me.items[i]['price'])/100)  +  (me.items[i]['price']*me.items[i]['qty'])   )/100);
			me.t_discount	= me.t_discount + (me.items[i]['discount']*(me.items[i]['price']*me.items[i]['qty'])/100);
			me.t_grand		= me.t_price + me.t_scharge + me.t_tax - me.t_discount ;
		});	
	}
	
	me.get_summary_val = function (s){
		var value = null;
		if (me.hasOwnProperty(s)) {
				value = me[s];
		}
		return value;
	}
	
	/*------------------double id handling----------------*/
	me.hasItem = function ( item ) {
		for( var current in me.items ) {
			var testItem = me.items[current];
			var matches = true;
			for( var field in item ){
				if( typeof( item[field] ) != "function"	&& 
					field != "qty"  				&& 
					field != "id" 						){
					if( item[field] != testItem[field] ){
						matches = false;
					}
				}	
			}
			if( matches ){ 
				return current; 
			}
		}
		return false;
	};
	
	
	me.search_indexbyid = function(id) {
		for (var i = 0, len = me.items.length; i < len; i++) {
		//alert('search idx by id : '+id+' == '+me.items[i].id);
        if (me.items[i].id === id)
            return i;
		}
		return -1; 
	}
	
	me.objectIdx_FindByKey = function(myarray, key, value) {
	//alert('Key : '+key+' Val : '+value);
		var tmp = null;
		for (var i = 0; i < myarray.length; i++) {
			//alert('Key : '+myarray[i][key]+' == Val : '+value);
			if (myarray[i][key] == value) {
				tmp = i;
				//alert ("tmp : "+tmp+" i : "+i);
			}
		}
		//alert(tmp);
		return tmp;
	}
	
	
	/******************************************************/
	/*            Init Bill Object                        */
	/******************************************************/
	me.init();
	
	
});